﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model.Enum;
using System.Text;

namespace HZ.Supervise.Web.Company
{
    public partial class TradingStatisticsCompanyPSBySQ : BasePage
    {



        public TradingStatisticsCompanyPSBySQ()
        {
            this.ModuleNo = Module.配送企业汇总统计.ToString();
            this.PageOperateType = OperateType.社区医疗机构.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.AreaControl1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;

            if (!IsPostBack)
            {
                BindData();
            }
        }

        void QueryControl1_OnQueryClick(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }




        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private SuperviseQuery QueryBy()
        {
            SuperviseQuery vs = new SuperviseQuery();

            #region 查询方法



            //System.Text.StringBuilder sb = new System.Text.StringBuilder();

            if (cbProductNameLike.Checked == true)
            {

                vs.Flag = "1";
            }
            else
            {
                vs.Flag = "0";
            }

            if (HZ.Common.zhHansUtil.HaveChineseCode(txtProductName.Text.Trim()) == true)
            {
                vs.ProductName = this.txtProductName.Text.Trim();
            }
            else
            {
                vs.PinyinCode = this.txtProductName.Text.Trim();

            }



            string timeFrom = string.Intern(this.txtTimeFrom.Text.Trim());

            string timeTo = string.Intern(this.txtTimeTo.Text.Trim());

            if (!string.IsNullOrEmpty(timeFrom))
            {
                vs.CenSubmiitTimeFrom = Convert.ToDateTime(timeFrom);

            }

            if (!string.IsNullOrEmpty(timeTo))
            {
                vs.CenSubmiitTimeTo = Convert.ToDateTime(timeTo);
            }

            if (!string.IsNullOrEmpty(this.txtMedicineModel.Text.Trim()))
            {
                vs.MedicineModelName = txtMedicineModel.Text.Trim();
            }
            if (!string.IsNullOrEmpty(this.txtPack.Text.Trim()))
            {
                vs.Pack = txtPack.Text.Trim();
            }
            if (!string.IsNullOrEmpty(this.txtUnit.Text.Trim()))
            {
                vs.Unit = txtUnit.Text.Trim();

            }
            if (!string.IsNullOrEmpty(this.txtHospitalName.Text.Trim()))
            {
                vs.HospitalName = txtHospitalName.Text.Trim();
            }
            string areaid = AreaControl1.SelectedAreaID;
            if (!string.IsNullOrEmpty(areaid) && areaid != "510000")
            {
                vs.AreaID = areaid;

            }
            else
            {
                vs.AreaID = "13";

            }


            if (!string.IsNullOrEmpty(txtAmountFrom.Text.Trim()))
            {
                vs.WarehouseAmountFrom = Convert.ToDecimal(txtAmountFrom.Text.Trim());


            }
            if (!string.IsNullOrEmpty(txtAmountTo.Text.Trim()))
            {
                vs.WarehouseAmountTo = Convert.ToDecimal(txtAmountTo.Text.Trim());


            }
            //IsCountry表示是否基层医疗机构：0 非基层医疗机构，1乡镇，2社区，3 民营，4 其他
            vs.IsCountry= "'2'";

            vs.Append = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;
            return vs;
            #endregion
        }


        public void BindData()
        {
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

            int t = 0;
            System.Data.DataTable dt = ie.CompanyPS_InfoByType(QueryBy(), this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);

            AspNetPager1.RecordCount = t;
            gvwInfo.PageSize = AspNetPager1.PageSize;
            this.gvwInfo.DataSource = dt;

            this.gvwInfo.DataBind();
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            BindData();

            GetCompany_PSCount();

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //按详情
            if (e.CommandName == "DierectByDtail")
            {
                //设置分组查询字段GroupSelectField
                if (ShowColumns.ShowColumn == null)
                {
                    ShowColumns.ShowColumn = new ShowColumns();
                    ShowColumns.ShowColumn.GroupFields = GroupFields.PurchaseCountTotal | GroupFields.PriceAvg | GroupFields.PurchaseAmountTotal | GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.WareHouseCountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate;
                }
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];

                //query.CompanyID_PS = e.CommandArgument.ToString();
                string[] company_PS = e.CommandArgument.ToString().Split(',');
                query.CompanyID_PS = company_PS[0].ToString();
                query.CompanyName_PS = company_PS[1].ToString();
                query._QueryPrompt = query._QueryPrompt + " " + company_PS[1].ToString();

                Session[sessionid] = query;
                string url = System.Configuration.ConfigurationManager.AppSettings["HosComDrugs"];
                Response.Redirect(string.Format("{0}?sessionid={1}&QueryPrompt={2}", url, sessionid, company_PS[1].ToString()));
            }

            //按医院查看
            if (e.CommandName == "DirectByHospital")
            {
                //设置分组查询字段GroupSelectField
                if (ShowColumns.ShowColumn == null)
                {
                    ShowColumns.ShowColumn = new ShowColumns();
                    ShowColumns.ShowColumn.GroupFields = GroupFields.PurchaseCountTotal | GroupFields.PriceAvg | GroupFields.PurchaseAmountTotal | GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.WareHouseCountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate | GroupFields.CountOrder;
                }

                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];

                //query.CompanyID_PS = e.CommandArgument.ToString();
                string[] company_PS = e.CommandArgument.ToString().Split(',');
                query.CompanyID_PS = company_PS[0].ToString();
                query.CompanyName_PS = company_PS[1].ToString();
                query._Titile = "配送企业相关医院";
                query._QueryPrompt = query._QueryPrompt + " " + company_PS[1].ToString();


                Session[sessionid] = query;
                string url = System.Configuration.ConfigurationManager.AppSettings["HospitalID"];
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
            }

            //按药品查看
            if (e.CommandName == "DierectByDrug")
            {
                //设置分组查询字段GroupSelectField
                if (ShowColumns.ShowColumn == null)
                {
                    ShowColumns.ShowColumn = new ShowColumns();
                    ShowColumns.ShowColumn.GroupFields = GroupFields.PurchaseCountTotal | GroupFields.PriceAvg | GroupFields.PurchaseAmountTotal | GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.WareHouseCountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate | GroupFields.CountOrder;
                }

                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];

                //query.CompanyID_PS = e.CommandArgument.ToString();
                string[] company_PS = e.CommandArgument.ToString().Split(',');
                query.CompanyID_PS = company_PS[0].ToString();
                query.CompanyName_PS = company_PS[1].ToString();
                query._Titile = "配送企业相关药品";
                query._QueryPrompt = query._QueryPrompt + " " + company_PS[1].ToString();

                Session[sessionid] = query;
                string url = System.Configuration.ConfigurationManager.AppSettings["ProcureCatalogID"];
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
            }

        }


        #region   数据准备

        /// <summary>
        /// 屏蔽相关查询连接
        /// </summary>
        private void SetColumnVisible()
        {
            //if (!string.IsNullOrEmpty(QueryControl1.ProcureCatalogID))
            //{
            //    foreach (GridViewRow row in gvwInfo.Rows)
            //    {
            //        row.FindControl("lbnDrug").Visible = false;
            //    }
            //}
            //if (!string.IsNullOrEmpty(QueryControl1.HospitalID))
            //{
            //    foreach (GridViewRow row in gvwInfo.Rows)
            //    {
            //        row.FindControl("lbnHos").Visible = false;
            //    }
            //}

        }

        #endregion

        protected void ddlOther_SelectedIndexChanged(object sender, EventArgs e)
        {
            string stkey = "";
            string sessionid = string.Empty;
            GridViewRow row = (GridViewRow)((Control)sender).Parent.Parent;
            DropDownList ddl = (DropDownList)row.FindControl("ddlOther");
            stkey = ddl.SelectedValue;
            if (stkey != "")
            {
                sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];
                //query.CompanyID_PS = row.Cells[1].Text; //((LinkButton)row.FindControl("lbnHos")).CommandArgument;
                //query.CompanyName_PS = row.Cells[2].Text;
                query.CompanyID_PS = gvwInfo.DataKeys[row.RowIndex].Values["CompanyID_PS"].ToString();
                query.CompanyName_PS = gvwInfo.DataKeys[row.RowIndex].Values["CompanyName_PS"].ToString();
                query._QueryPrompt = query._QueryPrompt + " " + gvwInfo.DataKeys[row.RowIndex].Values["CompanyName_PS"].ToString();

                Session[sessionid] = query;
                string url = System.Configuration.ConfigurationManager.AppSettings[stkey];
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
            }
        }

        ///// <summary>
        ///// 设置图表
        ///// </summary>
        ///// <param name="?"></param>
        //private void setChart(System.Data.DataTable dt)
        //{
        //    if (dt.Rows.Count > 0)
        //    {
        //        HZ.Supervise.Web.WebService.ChartConfig serChart = new HZ.Supervise.Web.WebService.ChartConfig();
        //        dt.TableName = "配送企业";
        //        serChart.DataSource = dt;
        //        if (ShowColumns.ShowColumn == null)
        //        {
        //            serChart.XName = "CompanyName_PS";
        //            serChart.YNames = "PurchaseAmountTotal";
        //        }
        //        else
        //        {
        //            string[] _columns = ShowColumns.ShowColumn.GroupFields.ToString().Split(',');
        //            serChart.XName = "CompanyName_PS";
        //            serChart.YNames = _columns[0];
        //        }
        //        serChart.ChartTypes = HZ.Supervise.Web.WebService.ChartType.PieChart;

        //        ChartControl1.webChart = serChart;
        //        ChartControl1.PageId = Guid.NewGuid().ToString();
        //        ChartControl1.EntranceChart();
        //    }
        //    else
        //    {
        //        ChartControl1.Visible = false;
        //    }
        //}

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
            int t = 0;
            System.Data.DataTable dt = ie.CompanyPS_InfoByType(QueryBy(), 0, 0, out t);

            ExportFile.Instance.ExportData(dt, gvwInfo, this.Page);

        }

        protected void lbtnSee_Click(object sender, EventArgs e)
        {
            //if (this.lbtnSee.Text == "显示图表")
            //{
            //    ChartControl1.Visible = true;
            //    this.lbtnSee.Text = "隐藏图表";
            //}
            //else
            //{
            //    ChartControl1.Visible = false;
            //    this.lbtnSee.Text = "显示图表";
            //}
        }

        /// <summary>
        /// 获取配送企业的个数
        /// </summary>
        public void GetCompany_PSCount()
        {

        }
    }
}
